﻿Imports System.Collections.Generic
Imports System.Security.Claims
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Data.OleDb
Imports System.Data.Entity.Validation

Public Class sms_info_about
    Inherits System.Web.UI.Page




    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            ' bindgrid()
        End If
    End Sub


    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Button1.Text = "Save" Then

            If (about_website.Text <> "" And contact_website.Text <> "") Then
                If (hdf.Value <> "") Then
                    Try

                        Dim filename As String
                        Dim img1 As String
                        Dim img2 As String
                        Dim img3 As String
                        Dim img4 As String
                        Dim img5 As String
                        Dim img6 As String


                        If (fl1.HasFile) Then
                            filename = System.DateTime.Now.GetHashCode().ToString()
                            img1 = filename + fl1.FileName
                            Try
                                fl1.PostedFile.SaveAs(Server.MapPath("~/Upload/") + img1)
                            Catch exc As Exception
                            End Try
                        Else
                            img1 = txtfl1.Text
                        End If

                        If (fl2.HasFile) Then
                            filename = System.DateTime.Now.GetHashCode().ToString()
                            img2 = filename + fl2.FileName
                            Try
                                fl2.PostedFile.SaveAs(Server.MapPath("~/Upload/") + img2)
                            Catch exc As Exception
                            End Try
                        Else
                            img2 = txtfl2.Text
                        End If

                        If (fl3.HasFile) Then
                            filename = System.DateTime.Now.GetHashCode().ToString()
                            img3 = filename + fl3.FileName
                            Try
                                fl3.PostedFile.SaveAs(Server.MapPath("~/Upload/") + img3)
                            Catch exc As Exception
                            End Try
                        Else
                            img3 = txtfl3.Text
                        End If

                        If (fl4.HasFile) Then
                            filename = System.DateTime.Now.GetHashCode().ToString()
                            img4 = filename + fl4.FileName
                            Try
                                fl4.PostedFile.SaveAs(Server.MapPath("~/Upload/") + img4)
                            Catch exc As Exception
                            End Try
                        Else
                            img4 = txtfl4.Text
                        End If

                        If (fl5.HasFile) Then
                            filename = System.DateTime.Now.GetHashCode().ToString()
                            img5 = filename + fl5.FileName
                            Try
                                fl5.PostedFile.SaveAs(Server.MapPath("~/Upload/") + img5)
                            Catch exc As Exception
                            End Try
                        Else
                            img5 = txtfl5.Text
                        End If
                        If (fl6.HasFile) Then
                            filename = System.DateTime.Now.GetHashCode().ToString()
                            img6 = filename + fl6.FileName
                            Try
                                fl6.PostedFile.SaveAs(Server.MapPath("~/Upload/") + img6)
                            Catch exc As Exception
                            End Try
                        Else
                            img6 = txtfl6.Text
                        End If

                        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString004").ConnectionString)
                        Dim SqlString As String = "update  sms_info set about_website=@f1, contact_website=@f2, about_image1=@f3, about_image2=@f4,about_image3=@f5,about_image4=@f6,about_image5=@f7,about_image6=@f8 where ID= " + hdf.Value
                        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

                        cmd.CommandType = CommandType.Text
                        cmd.Parameters.AddWithValue("@f1", about_website.Text)
                        cmd.Parameters.AddWithValue("@f2", contact_website.Text)
                        cmd.Parameters.AddWithValue("@f3", img1)
                        cmd.Parameters.AddWithValue("@f4", img2)
                        cmd.Parameters.AddWithValue("@f5", img3)
                        cmd.Parameters.AddWithValue("@f6", img4)
                        cmd.Parameters.AddWithValue("@f7", img5)
                        cmd.Parameters.AddWithValue("@f8", img6)
                        oleDbConn.Open()
                        cmd.ExecuteNonQuery()
                        GridView1.DataSourceID = "SqlDataSource005"
                        GridView1.DataBind()


                    Catch ex As DbEntityValidationException

                    End Try
                    GridView1.EditIndex = -1
                    Clear()
                    Button1.Text = "Edit"
                    Disable()
                    ' bindgrid()
                End If
            End If
        ElseIf Button1.Text = "Edit" Then
            Enable()
            Button1.Text = "Save"
            Button1.ValidationGroup = "add"


        End If


        '    ' New Code - sms_info_contact_info.aspx.vb - 12 Jan 2014 - Gene Da Rocha - Adding New Data to SMS_INFO table.

        '    Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString004").ConnectionString)
        '    Dim SqlString As String = "Insert into sms_info(about_website,contact_website,about_image1,about_image2,about_image3,about_image4,about_image5,about_image6) Values(@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8)"
        '    Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)


        '    cmd.Parameters.AddWithValue("@f1", about_website.Text)
        '    cmd.Parameters.AddWithValue("@f2", contact_website.Text)
        '    cmd.Parameters.AddWithValue("@f3", about_image1.Text)
        '    cmd.Parameters.AddWithValue("@f4", about_image2.Text)
        '    cmd.Parameters.AddWithValue("@f5", about_image3.Text)
        '    cmd.Parameters.AddWithValue("@f6", about_image4.Text)
        '    cmd.Parameters.AddWithValue("@f7", about_image5.Text)
        '    cmd.Parameters.AddWithValue("@f8", about_image6.Text)
        '    oleDbConn.Open()
        '    cmd.ExecuteNonQuery()
        '    oleDbConn.Close()
        '     labelrecord.Text = "Record Added"
        '    about_website.Text = ""
        '    contact_website.Text = ""
        '    about_image1.Text = ""
        '    about_image2.Text = ""
        '    about_image3.Text = ""
        '    about_image4.Text = ""
        '    about_image5.Text = ""
        '    about_image6.Text = ""
        '    'labelrecord.Text = ""
        '    Response.Redirect("sms_info_about.aspx")
    End Sub

    Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)
        Dim id As Integer
        id = Convert.ToInt32(GridView1.DataKeys(e.NewEditIndex).Value)



        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
        Dim SqlString As String = "select * from sms_info where ID =  @ID"

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)


        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@ID", id)

        oleDbConn.Open()

        Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        Dim ds As DataSet = New DataSet()
        sda.Fill(ds)


        Dim dr As DataRow = ds.Tables(0).Rows(0)

        about_website.Text = dr("about_website").ToString()
        contact_website.Text = dr("contact_website").ToString()
        about_image1.Text = dr("about_image1").ToString()
        about_image2.Text = dr("about_image2").ToString()
        about_image3.Text = dr("about_image3").ToString()
        about_image4.Text = dr("about_image4").ToString()
        about_image5.Text = dr("about_image5").ToString()
        about_image6.Text = dr("about_image6").ToString()
        txtfl1.Text = dr("about_image1").ToString()
        txtfl2.Text = dr("about_image2").ToString()
        txtfl3.Text = dr("about_image3").ToString()
        txtfl4.Text = dr("about_image4").ToString()
        txtfl5.Text = dr("about_image5").ToString()
        txtfl6.Text = dr("about_image6").ToString()
        hdf.Value = dr("ID").ToString()
        Disable()
        Button1.Text = "Edit"



    End Sub
    Private Sub Disable()
        about_website.Enabled = False
        contact_website.Enabled = False
        txtfl1.Visible = False
        txtfl2.Visible = False
        txtfl3.Visible = False
        txtfl4.Visible = False
        txtfl5.Visible = False
        txtfl6.Visible = False
        divfl1.Visible = False
        divfl2.Visible = False
        divfl3.Visible = False
        divfl4.Visible = False
        divfl5.Visible = False
        divfl6.Visible = False
        about_image1.Visible = True
        about_image2.Visible = True
        about_image3.Visible = True
        about_image4.Visible = True
        about_image5.Visible = True
        about_image6.Visible = True
    End Sub

    Private Sub Enable()
        about_website.Enabled = True
        contact_website.Enabled = True
        txtfl1.Visible = True
        txtfl2.Visible = True
        txtfl3.Visible = True
        txtfl4.Visible = True
        txtfl5.Visible = True
        txtfl6.Visible = True
        divfl1.Visible = True
        divfl2.Visible = True
        divfl3.Visible = True
        divfl4.Visible = True
        divfl5.Visible = True
        divfl6.Visible = True
        about_image1.Visible = False
        about_image2.Visible = False
        about_image3.Visible = False
        about_image4.Visible = False
        about_image5.Visible = False
        about_image6.Visible = False
    End Sub


    Protected Sub btncancel_Click(sender As Object, e As EventArgs)
        Button1.Text = "Edit"
        Disable()


    End Sub

    Private Sub Clear()

        hdf.Value = ""
        about_website.Text = ""
        contact_website.Text = ""
        txtfl1.Text = ""
        txtfl2.Text = ""
        txtfl3.Text = ""
        txtfl4.Text = ""
        txtfl5.Text = ""
        txtfl6.Text = ""
        divfl1.Visible = True
        divfl2.Visible = True
        divfl3.Visible = True
        divfl4.Visible = True
        divfl5.Visible = True
        divfl6.Visible = True
        about_image1.Text = ""
        about_image2.Text = ""
        about_image3.Text = ""
        about_image4.Text = ""
        about_image5.Text = ""
        about_image6.Text = ""
    End Sub
    Private Sub bindgrid()
        Try
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
            Dim SqlString As String = "SELECT [ID], [about_website], [contact_website], [about_image1], [about_image2], [about_image3], [about_image4], [about_image5], [about_image6] FROM [sms_info] order by ID DESC"

            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
            Dim ds As DataSet = New DataSet()
            sda.Fill(ds)

            GridView1.DataSource = ds
            GridView1.DataBind()



        Catch ex As DbEntityValidationException

        End Try

    End Sub

    Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
        Try
            Dim id As Integer
            id = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Value)

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
            Dim SqlString As String = "delete  FROM sms_info where ID=@ID"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@ID", id)


            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            ' bindgrid()


        Catch ex As DbEntityValidationException

        End Try
    End Sub
    Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)

        GridView1.PageIndex = e.NewPageIndex
    End Sub
End Class